package com.idata.service.services.impl;

import cn.hutool.json.JSONObject;
import com.idata.model.response.DisputeWordResponse;
import com.idata.service.config.WordHotProperties;
import com.idata.service.services.DisputeHotWordService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class DisputeHotWordServiceWebImpl implements DisputeHotWordService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DisputeHotWordServiceWebImpl.class);

    @Autowired
    @Qualifier("hotWordsRestTemplate")
    private RestTemplate restTemplate;

    @Value("${spring.profiles.active}")
    private String activeProfile;

    @Autowired
    private WordHotProperties wordHotProperties;

    @Override
    public List<DisputeWordResponse> getHotWords(String text) {
        // 如果是dev环境，就直接mock。如果没配置，则报错
        if (StringUtils.equals(activeProfile, "dev")) {
            return DisputeWordResponse.mock();
        }
        HttpHeaders header = new HttpHeaders();
        header.setContentType(MediaType.APPLICATION_JSON);

        WordHotRequest request = new WordHotRequest();
        request.setWord(text);
        request.setMode(this.wordHotProperties.getMode());


        HttpEntity<WordHotRequest> httpEntity = new HttpEntity<>(request, header);
        List<DisputeWordResponse> result = new ArrayList<>();
        ResponseEntity<JSONObject> responseEntity = this.restTemplate.postForEntity(this.wordHotProperties.getUrl(), httpEntity, JSONObject.class);
        if (responseEntity.getStatusCodeValue() != 200) {
            LOGGER.error(String.format("failed to invoke HotWord server, mode: %s, url: %s, result: %s", this.wordHotProperties.getMode(), this.wordHotProperties.getUrl(), responseEntity.getBody()));
            return new ArrayList<>();
        }
        Map<String, Integer> hotWords = responseEntity.getBody().get("data", Map.class);
        for (Map.Entry<String, Integer> entry : hotWords.entrySet()) {
            result.add(new DisputeWordResponse(entry.getKey(), entry.getValue()));
        }
        Collections.sort(result);
        return result;
    }

    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    private static class WordHotRequest {

        // 案件原由{不用填，查数据库}
        private String word;
        // 模式 {可选为：jieba,}
        private String mode;

    }

}
